# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pybind11
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.0
pkgrel=1
pkgdesc="A lightweight header-only library that exposes C++ types in Python and vice versa (mingw-w64)"
url='https://pybind11.readthedocs.org/'
license=('BSD')
arch=('any')
optdepends=("${MINGW_PACKAGE_PREFIX}-python3: to target bindings supporting python 3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-breathe"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx_rtd_theme"
             "${MINGW_PACKAGE_PREFIX}-python3-pytest")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-pytest")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/pybind/pybind11/archive/v${pkgver}.tar.gz")
sha256sums=('97504db65640570f32d3fdf701c25a340c8643037c3b69aec469c10c93dc8504')

build () {
  cd "${srcdir}/${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/python3 setup.py build

  cd "${srcdir}/${_realname}-${pkgver}/build"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python3 \
    ..
  make mock_install

  cd "${srcdir}/${_realname}-${pkgver}/docs"
  make man
}

package() {
  local _pythonver
  _pythonver="$(${MINGW_PREFIX}/bin/python3 --version | awk '{ print $2 }' | grep -o '^[0-9]*\.[0-9]*')"
  
  # python3 modules
  cd "${srcdir}/${_realname}-${pkgver}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install_lib --install-dir="${pkgdir}${MINGW_PREFIX}/lib/python${_pythonver}/site-packages" --optimize='1'

  # C++ headers
  cd "${srcdir}/${_realname}-${pkgver}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install_headers --install-dir="${pkgdir}${MINGW_PREFIX}/include/${_realname}"
  
  # cmake files
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/cmake/${_realname}"
  cd build/mock_install/share/cmake/pybind11
  install -m644 *.cmake "${pkgdir}${MINGW_PREFIX}/share/cmake/${_realname}"
  
  # manpage
  cd "${srcdir}/${_realname}-${pkgver}/docs/.build/man"
  install -D -m644 "${_realname}.1" -t "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  
  # license
  install -D -m644 ${srcdir}/${_realname}-${pkgver}/LICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
